package com.example.dllo.moondog.contact;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.example.dllo.moondog.R;
import com.example.dllo.moondog.base.BaseFragment;
import com.example.dllo.moondog.chat.ChatDetailActivity;

import java.util.List;


/**
 * Created by dllo on 16/3/2.
 */
public class ContactFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private Button btnAdd;
    private ListView userListView;
    private UserListViewAdapter adapter;
    private List<String> usernames = null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);
        btnAdd = (Button) view.findViewById(R.id.btn_addfriend);
        userListView = (ListView) view.findViewById(R.id.listview_fragment_contact);

        EMChatOptions options = EMChatManager.getInstance().getChatOptions();
        options.setAcceptInvitationAlways(false);
        options.setUseRoster(true);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        try {
            usernames = EMContactManager.getInstance().getContactUserNames();
        } catch (EaseMobException e) {
            e.printStackTrace();
        }

        adapter = new UserListViewAdapter(usernames,getActivity());
        userListView.setAdapter(adapter);
        userListView.setOnItemClickListener(this);
        btnAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(),AddFriendActivity.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), ChatDetailActivity.class);
        intent.putExtra("username",usernames.get(position));
        startActivity(intent);
    }
}
